package com.shanghaionstar.smart.presenter;

import android.content.Intent;
import android.text.TextUtils;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.smart.GetSmartDeviceResponse;
import com.onstar.cn.smart.SmartDevicesResponse;
import com.onstar.cn.sqlite.entities.LandingsPageEntity;
import com.shanghaionstar.smart.SmartAddDeviceFrg;
import com.shanghaionstar.smart.SmartDeviceListFrg;
import com.shanghaionstar.smart.SmartEntranceFrg;
import com.shanghaionstar.smart.iview.ISmartHomeView;
import com.shanghaionstar.smart.service.SmartHomeService;

import java.util.List;

import rx.functions.Action1;

/**
 * Created by 7828wc on 2016/6/22.
 */
public class SmartHomePresenter extends BasePresenter<LandingsPageEntity> {
    private BaseActivity mContext;
    private ISmartHomeView mSmartHomeView;
    private SmartDevicesResponse mSmartDevicesRes;
    private SmartHomeService service;

    public SmartHomePresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<LandingsPageEntity> view) {
        super.setView(view);
        mSmartHomeView = (ISmartHomeView) view;
        mContext = (BaseActivity) mSmartHomeView.onActivity();
        service = new SmartHomeService(mContext);
    }

    /**
     * 根据是否存在设备列表显示相关fragment
     */
    public void checkDeviceList() {
        mSmartHomeView.setLoading(true);
        subscription = service.getSmartDevices().subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<SmartDevicesResponse>() {
                    @Override
                    public void call(SmartDevicesResponse mSmartDevicesRes) {
                        mSmartHomeView.setLoading(false);
                        List<GetSmartDeviceResponse> mSmartDevices = mSmartDevicesRes.devices;
                        if (mSmartDevices != null && !mSmartDevices.isEmpty()) {
                            switchToDeviceList();
                        } else {
                            switchToHomeApplianceFrg();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mSmartHomeView.setLoading(false);
                        if (throwable != null)
                            mSmartHomeView.onResponseError((OnStarException) throwable);
                        switchToDeviceList();
                    }
                });
    }


//    /**
//     * @param json 处理后台返回的Json串
//     */
//    private void processJson(String json) {
//        if (json == null) {
//            switchToHomeApplianceFrg();
//            return;
//        }
//        mSmartDevicesRes = new Gson().fromJson(json, SmartDevicesResponse.class);
//        List<GetSmartDeviceResponse> mSmartDevices = mSmartDevicesRes.devices;
//        if (mSmartDevices != null && !mSmartDevices.isEmpty()) {
//            switchToDeviceList();
//        } else {
//            switchToHomeApplianceFrg();
//        }
//    }

    public void switchToHomeApplianceFrg() {
        SmartEntranceFrg homeApplianceSettingFrg = new SmartEntranceFrg();
        mContext.changeFragment(mContext.getSupportFragmentManager(), homeApplianceSettingFrg, SmartEntranceFrg.class, true);
    }

    public void switchToDeviceList() {
        SmartDeviceListFrg deviceListFrg = new SmartDeviceListFrg();
        mContext.changeFragment(mContext.getSupportFragmentManager(), deviceListFrg, SmartDeviceListFrg.class, true);
    }

    public void onActivityResult(Intent data) {
        String qRCode = data.getStringExtra("result");
        if (!TextUtils.isEmpty(qRCode))
            handleCode(qRCode);
    }

    /**
     * @param qRCode 扫描成功后重添加页跳转到连接页面
     */
    private void handleCode(String qRCode) {
        SmartAddDeviceFrg frg = (SmartAddDeviceFrg) mContext.getFragment(SmartAddDeviceFrg.class);
        if (frg != null) {
            frg.goToConnect(qRCode);
        }
    }

}



